package com.itlong.cloud.tapd.util;

import com.itlong.cloud.tapd.common.Result;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * <des>
 *
 * <des>
 *
 * @create Date 2021-09-07
 **/
public class HttpSendUtil {


    static RestTemplate restTemplate = new RestTemplate();

//    /**
//    * <desc>
//    *      发送http请求
//    * </desc>
//    * @param url
//    * @param param  参数
//    * @return: com.itlong.cloud.tapd.common.Result
//    * @author LiuYu
//    * @date: 2021-09-07
//     **/
//    public static Result send(String url, HashMap<String, Object> param){
//        RestTemplate restTemplate = new RestTemplate();
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        headers.add("Host","api.tapd.cn");
//        headers.add("Authorization","Basic NWZOcUVPSzM6QTc1OENDNzQtMkYzMS1DOEJELUM5MDEtOTEyOTQyRDUzMkVE");
//        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map,headers);
//        ResponseEntity<Result> entity = restTemplate.exchange(url,HttpMethod.POST,httpEntity,Result.class,param);
//        Result result = entity.getBody();
//        return result;
//    }

    /**
     * <desc>
     *      发送http请求POST
     * </desc>
     * @param url
     * @return: com.itlong.cloud.tapd.common.Result
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public static Result send(String url){
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Authorization","Basic NWZOcUVPSzM6QTc1OENDNzQtMkYzMS1DOEJELUM5MDEtOTEyOTQyRDUzMkVE");
        ResponseEntity<Result> entity = restTemplate.exchange(url,HttpMethod.POST,new HttpEntity<>(headers),Result.class);
        Result result = entity.getBody();
        return result;
    }

    /**
     * <desc>
     *      发送http请求GET
     * </desc>
     * @param url
     * @return: com.itlong.cloud.tapd.common.Result
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public static Result getSend(String url){
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Host","api.tapd.cn");
        headers.add("Authorization","Basic NWZOcUVPSzM6QTc1OENDNzQtMkYzMS1DOEJELUM5MDEtOTEyOTQyRDUzMkVE");
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map,headers);
        ResponseEntity<Result> entity = restTemplate.exchange(url,HttpMethod.GET,httpEntity,Result.class);
        Result result = entity.getBody();
        return result;
    }
}